<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\MakeModel;
use app\admin\model\FieldModel;
use app\admin\model\CountryModel;
use app\admin\model\SeriesModel;

class Make extends Base
{
	/**
	 * 制造商对象
	 * @return mixed
	 */
	public $make_obj;
	
	/**
	 * 应用领域对象
	 * @return mixed
	 */
	public $field_obj;
	
	
	/**
	 * 国家区域对象
	 * @return mixed
	 */
	public $country_obj;
	
	/**
	 * 系列对象
	 * @return mixed
	 */
	public $series_obj;
	
	/**
	 * 构造函数
	 * @return mixed
	 */
	public function __construct()
	{
		parent::__construct();
		$this->make_obj = new MakeModel();
		$this->field_obj = new FieldModel();
		$this->country_obj = new CountryModel();
		$this->series_obj = new SeriesModel();				
	}
	
	/**
	 * 制造商
	 * @return mixed
	 */
    public function lists()
    {
		$show_file_dir = config('show_file_dir');
		$input = input('get.');
		$fieldData = array();
		$field = FieldModel::all();
		foreach($field as $_f){
			$fieldData[$_f['id']] = $_f;
		}
		$countryData = array();
		$country = CountryModel::all();
		foreach($country as $_c){
			$countryData[$_c['id']] = $_c;
		}
		if(isset($input['data'])){
			$data = $this->make_obj->getMakeList($input);
			if(!empty($data['list']['data'])){
				foreach($data['list']['data'] as $key=>$val){
					$data['list']['data'][$key]['field_country_code'] = $fieldData[$val['field_id']]['field_code'].'-'.$countryData[$val['country_id']]['country_code'];
					$data['list']['data'][$key]['field_country_name'] = $fieldData[$val['field_id']]['field_name'].'-'.$countryData[$val['country_id']]['country_name'];
					if(!empty($val['make_url'])){
						$data['list']['data'][$key]['make_url'] = "<img src=".$show_file_dir.$val['make_url']." />";
					}
					if(is_numeric($val['create_time'])){
						$data['list']['data'][$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
					}
				}
			}
			$result = array('code'=>0,'msg'=>'','count'=>$data['list']['total'],'data'=>$data['list']['data']);
			return json($result);
		}else{
			$where = array();
			$where['export'] = 1;
			$data = $this->country_obj->getFieldCountryList($where);
			if(!empty($data['list'])){
				foreach($data['list'] as $key=>$val){
					$fieldObj = FieldModel::get($val['field_id']);
					$countryObj = CountryModel::get($val['country_id']);
					$data['list'][$key]['field_country_code'] = $fieldObj->field_code." - ".$countryObj->country_code;
					$data['list'][$key]['field_country_name'] = $fieldObj->field_name." - ".$countryObj->country_name;
				}
			}
			$this->assign('field_country',$data['list']);
			$this->assign('field', $fieldData);
			return view('index');
		}
    }
	
	/**
	 * 图片导入
	 * @return mixed
	 */
	public function import()
	{
		$post = request()->post();
		$upload_dir = config('upload_dir').$post['dir'].'/';
		$file = request()->file($post['u_type']);
		$info = $file->move($upload_dir);
		$data = array();
		if($info){
			$zip = new \ZipArchive; 
			if ($zip->open($upload_dir.$info->getSaveName()) === TRUE){  	
				$zip->extractTo($upload_dir);
				$zip->close();
				$name_info = $info->getInfo();
				$name_arr = pathinfo($name_info['name']);
				$files = scandir($upload_dir.$name_arr['filename']);
				foreach($files as $file){
					if($file !='.' && $file !='..'){
						$file_info = pathinfo($file);
						$file_name = $file_info['filename'];
						$make_obj = MakeModel::get(['make_code' => $file_name]);
						if($make_obj != NULL){
							MakeModel::where('make_code', $file_name)->update(['make_url' => $post['dir'].'/'.$name_arr['filename'].'/'.$file]);
						}
					}
				}
				$result = [
					'code' => 0,
					'msg'  => '导入成功！',
					'list' => array(),
					'data'=>$data
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '导入失败！',
					'list' => array(),
					'data'=>$data
				];
			}
		}else{
			$result = [
				'code' => 0,
				'msg'  => $file->getError(),
				'list' => array(),
				'data'=>$data
			];
		}
		return json($result);
	}
	
	/**
	 * 新增制造商页面
	 * @return mixed
	 */
	public function add()
	{
		return view('add');
	}
	/**
	 * 编辑制造商
	 * @return mixed
	 */
	public function edit()
	{
		$input = input('get.');
		$view = MakeModel::get($input['id'])->toArray();
		$this->assign('view', $view);
		return view('edit');
	}
	
	/**
	 * 更新制造商
	 * @return mixed
	 */
	public function save()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			if(isset($input['f_c_id'])){
				$where = array();
				$where['id'] = $input['f_c_id'];
				$data = $this->country_obj->getCountryRela($where);
				$input['field_id'] = $data[0]['field_id'];
				$input['country_id'] = $data[0]['country_id'];
			}
			if(isset($input['id']) && intval($input['id']) > 0){
				$input['update_time'] = time();
				$flag = $this->make_obj->save($input,['id'=>$input['id']]);
			}else{
				$input['create_time'] = time();
				$input['update_time'] = time();
				$this->make_obj->data($input);
				$flag = $this->make_obj->save();
			}
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新制造商数据，请求不合法！');
		}
	}	
	
	/**
	 * 删除制造商
	 * @return mixed
	 */
	public function del()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$where = array();
			$where['rela_status'] = 0;
			$where['make_id'] = $input['id'];
			$country = $this->series_obj->getSeriesList($where);
			if(!empty($country['list'])){
				$result = [
					'code' => 0,
					'msg'  => '制造商已经系列，不允许删除！',
					'list' => $input
				];
			}else{
				$field = MakeModel::get($input['id']);
				$field->make_status = -1;
				$flag = $field->save();
				if($flag){
					$result = [
						'code' => 1,
						'msg'  => '请求成功',
						'list' => $input
					];
				}else{
					$result = [
						'code' => 0,
						'msg'  => '请求失败',
						'list' => $input
					];
				}
			}
			return json($result);
		}else{
			$this->error('更新制造商数据，请求不合法！');
		}
	}
	
	/**
	 * 更新制造商状态
	 * @return mixed
	 */
	public function setStatus()
	{
		if ($this->request->isPost()) {
			$input = input('post.');
			$field = MakeModel::get($input['id']);
			$status = 1;
			if($field->make_status){
				$status = 0;
			}
			$field->make_status = $status;
			$flag = $field->save();
			if($flag){
				$result = [
					'code' => 1,
					'msg'  => '请求成功',
					'list' => $input
				];
			}else{
				$result = [
					'code' => 0,
					'msg'  => '请求失败',
					'list' => $input
				];
			}
			return json($result);
		}else{
			$this->error('更新制造商数据，请求不合法！');
		}
	}
}
